using Spine.Unity;
using System.Collections;
using UnityEngine;

namespace Spine.Unity.Examples {
    /// <summary>
    /// Raptor角色动画控制器示例类
    /// 演示Spine动画的混合播放与随机触发逻辑
    /// </summary>
    public class Raptor : MonoBehaviour {
        
        #region Inspector 配置区域
        [Header("动画资源")]
        [Tooltip("行走动画资源")]
        public AnimationReferenceAsset walk;
        
        [Tooltip("掏枪动画资源")]
        public AnimationReferenceAsset gungrab;
        
        [Tooltip("收枪动画资源")]
        public AnimationReferenceAsset gunkeep;
        #endregion

        private SkeletonAnimation skeletonAnimation;

        /// <summary>
        /// 初始化组件并启动动画协程
        /// </summary>
        void Start () {
            // 获取Spine动画组件
            skeletonAnimation = GetComponent<SkeletonAnimation>();
            
            // 启动枪械动作协程
            StartCoroutine(GunGrabRoutine());
        }

        /// <summary>
        /// 枪械动作控制协程
        /// 在行走动画基础上叠加随机枪械动作
        /// </summary>
        IEnumerator GunGrabRoutine () {
            
            // 在轨道0循环播放行走动画
            skeletonAnimation.AnimationState.SetAnimation(0, walk, true);
            
            // 无限循环执行枪械动作序列
            while (true) {
                // 随机等待0.5-3秒后触发掏枪动作
                yield return new WaitForSeconds(Random.Range(0.5f, 3f));
                skeletonAnimation.AnimationState.SetAnimation(1, gungrab, false);
                
                // 随机等待0.5-3秒后切换到收枪动画
                yield return new WaitForSeconds(Random.Range(0.5f, 3f));
                skeletonAnimation.AnimationState.SetAnimation(1, gunkeep, false);
            }
        }
    }
}